package pdf;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.nio.ByteBuffer;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

/**
 * @author jichao
 * @date 2022/10/31 17:20
 */
@RestController
@RequestMapping("/pdf")
@Validated
public class TestListController {

    public static String PNG_PATH = "C:\\Users\\PC-a\\Desktop\\nft.png";

    @GetMapping("/export")
    public byte[] getUserByName() throws IOException {
        ByteArrayOutputStream outputStream = null;
        InputStream pdfInputStream = null;
        PDDocument pdDocument = null;
        try {
            Map<String, Object> paramMap = new HashMap<>();
            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            paramMap.put("date_time", dateTimeFormatter.format(LocalDateTime.now()));
            paramMap.put("date", dateTimeFormatter.format(LocalDateTime.now()).substring(0, 10));
            /*String outPath = "D:\\A.pdf";*/
            String templateDirectory = "src/main/resources/templates";
            String templateContent = GetHtmlContent.getTemplateContent(templateDirectory, "generationpdf.html", paramMap);
            outputStream = GeneratePDF.html2Pdf(templateContent);

            byte[] bytes = outputStream.toByteArray();
            ByteBuffer buffer = ByteBuffer.wrap(bytes);
            pdfInputStream = new ByteArrayInputStream(buffer.array());

//        2、得到PDF文档对象
            pdDocument = PDDocument.load(pdfInputStream);
//        3、由Document得到Page对象
            PDPage page = pdDocument.getPage(0);
//        4、通过图片路径和PDF文档对象得到图片image对象
            PDImageXObject image = PDImageXObject.createFromFile(PNG_PATH, pdDocument);
//        5、创建pageStream对象
            PDPageContentStream pageStream = new PDPageContentStream(pdDocument, page, PDPageContentStream.AppendMode.APPEND, false, false);

//   6、pageStream对象绘制图片位置及大小，已PDF文件右下角为原点（x,y）是图片左下角左边，width、height是图片的长和宽
            pageStream.drawImage(image, 55, 625, 100, 100);

            pageStream.close();

//    7、PDF转换流输出
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            pdDocument.save(byteArrayOutputStream);
            byte[] pdfBytes = byteArrayOutputStream.toByteArray();
            return pdfBytes;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //      8、关闭流
            assert pdDocument != null;
            pdDocument.close();
            outputStream.close();
            pdfInputStream.close();
        }


        return new byte[0];
    }

    @GetMapping
    public byte[] getResource() throws IOException {
        org.apache.commons.io.output.ByteArrayOutputStream imgOutputStream = new org.apache.commons.io.output.ByteArrayOutputStream();
        byte[] captchaBytes;
        BufferedImage bufferedImage;
        bufferedImage = ImageIO.read(new File("C:\\Users\\PC-a\\Desktop\\nft.jpg"));

        BufferedImage newBufferedImage = new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(), BufferedImage.TYPE_INT_RGB);

        newBufferedImage.createGraphics().drawImage(bufferedImage, 0, 0, Color.white, null);

        ImageIO.write(newBufferedImage, "png", imgOutputStream);
        captchaBytes = imgOutputStream.toByteArray();





        return captchaBytes;



    }
}
